//
//  YWReplyManager.m
//  maintain
//
//  Created by Kyo on 16/6/29.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "MJExtension.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWReplyDBManager.h"
#import "YWMacro.h"

#import "YWHandler.h"
#import "YWReply.h"
#import "YWReplyManager.h"

@implementation YWReplyManager
YWSingletonM(Manager)

- (BOOL)updateReplyWithReplyArray : (NSArray*)replyArray
{
    return [[YWReplyDBManager shareManager] updateReplyWithReplyArray:replyArray];
}

- (BOOL)updateReplyIsReadWithType:(YWReplyRelationType)relationType
{
    return [[YWReplyDBManager shareManager] updateReplyIsReadWithType:relationType];
}

- (BOOL)deleteReplyWithReplyId:(NSInteger)replyId
{
    return [[YWReplyDBManager shareManager] deleteReplyWithReplyId:replyId];
}

- (NSArray*)replysWithtRelationId:(NSInteger)relationId andRelationType:(NSInteger)relationType
{
    return [[YWReplyDBManager shareManager] replysWithtRelationId:relationId andRelationType:relationType];
}

- (NSArray*)noReadReplysWithRelationType:(YWReplyRelationType)relationType
{
    return [[YWReplyDBManager shareManager] noReadReplysWithRelationType:relationType];
}

- (NSInteger)noReadReplysCountWithRelationType:(YWReplyRelationType)relationType{
    return [[YWReplyDBManager shareManager] noReadReplysCountWithRelationType:relationType];
}

- (NSInteger)replysCountWithtRelationId:(NSInteger)relationId relationType:(YWReplyRelationType)relationType{
    return [[YWReplyDBManager shareManager] replysCountWithtRelationId:relationId relationType:relationType];
}

- (void)commitReplys:(YWReply*)reply url:(NSString*)url WithComplete:(void (^)(BOOL))complete
{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    //NSString* requestUrl = [NSString stringWithFormat:@"%@/%ld", url, (long)reply.relationId];
    //NSDictionary* params = @{ @"note" : reply.content };
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:reply.content?:@"" forKey:@"content"];
    [params setValue:@(reply.atUser.uniqueId) forKey:[NSString stringWithFormat:@"atUser.uniqueId"]];
    [params setValue:@(reply.relationType) forKey:@"type"];
    [params setValue:@(reply.relationId) forKey:@"relationId"];
    YWLog(@"commitReply params %@",params);
    [[WMHttpTool shareHttpTool] postWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        YWReplySuccessJson* replySuccess = [YWReplySuccessJson mj_objectWithKeyValues:json];
        reply.replyId = replySuccess.replyId;
        BOOL save = [[YWReplyManager shareManager] updateReplyWithReplyArray:reply ? @[ reply ] : nil];
        if (save) {
            //            [[NSNotificationCenter defaultCenter] postNotificationName:@"reflashTempMission" object:nil];
        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"kTextCommitFailed", nil)];
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
        YWLog(@"%ld", (long)response.statusCode);
    }];
}

- (void)requestReplysWithRelationId:(NSString *)relationId type:(NSInteger)type WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headersDic = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headersDic;
    if (headersDic == nil) {if (complete) {complete(NO);}return;}
    
    NSMutableDictionary* params = [NSMutableDictionary dictionary];
    [params setValue:relationId?:@"" forKey:@"relationId"];
    [params setValue:@(type) forKey:@"type"];
    NSString *url = [YWCommonTool postReplyUrl];
    
    YWLog(@"requestReply:%@, params %@",url,params);
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* replyArray = [YWReply mj_objectArrayWithKeyValuesArray:json];
        if (replyArray.count) {
            [[YWReplyManager shareManager] updateReplyWithReplyArray:replyArray];
        }
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        if (complete) {
            complete(NO);
        }
//        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
//            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
//        }else{
//            [YWCommonTool showTextHUDWithContent:@"请求失败,请重试"];
//        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
        YWLog(@"%ld", (long)response.statusCode);
    }];
}

@end
